<?php

/**
 * This is the model class for table "{{profiles}}".
 */
class Profiles extends CActiveRecord
{
	/**
	 * The followings are the available columns in table '{{profiles}}':
	 * @var integer $user_id
	 * @var string $qq
	 * @var string $twitter
	 * @var string $phone
	 * @var string $web
	 * @var string $city
	 * @var string $proviencd
	 * @var string $town
	 * @var string $gender
	 * @var integer $birth_day
	 * @var integer $birt_month
	 * @var integer $birth_year
	 * @var string $about_me
	 * @var string $register_time
	 * @var string $last_login_time
	 * @var string $last_login_ip
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return Profiles the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{profiles}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('register_time, last_login_time, last_login_ip', 'required','on'=>'register'),
			array('birth_day, birth_month, birth_year', 'numerical', 'integerOnly'=>true),
			array('qq, phone, register_time, last_login_time', 'length', 'max'=>20),
			array('twitter, city, proviencd, town, last_login_ip', 'length', 'max'=>50),
			array('web', 'length', 'max'=>30),
			array('gender', 'length', 'max'=>5),
			array('about_me', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, qq, twitter, phone, web, city, proviencd, town, gender, birth_day, birth_month, birth_year, about_me, register_time, last_login_time, last_login_ip', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'qq' => 'Qq',
			'twitter' => 'Twitter',
			'phone' => 'Phone',
			'web' => 'Web',
			'city' => 'City',
			'proviencd' => 'Proviencd',
			'town' => 'town',
			'gender' => 'Gender',
			'birth_day' => 'Birth Day',
			'birth_month' => 'Birth Month',
			'birth_year' => 'Birth Year',
			'about_me' => 'About Me',
			'register_time' => 'Register Time',
			'last_login_time' => 'Last Login Time',
			'last_login_ip' => 'Last Login Ip',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);

		$criteria->compare('qq',$this->qq,true);

		$criteria->compare('twitter',$this->twitter,true);

		$criteria->compare('phone',$this->phone,true);

		$criteria->compare('web',$this->web,true);

		$criteria->compare('city',$this->city,true);

		$criteria->compare('proviencd',$this->proviencd,true);

		$criteria->compare('town',$this->town,true);

		$criteria->compare('gender',$this->gender,true);

		$criteria->compare('birth_day',$this->birth_day);

		$criteria->compare('birth_month',$this->birt_month);

		$criteria->compare('birth_year',$this->birth_year);

		$criteria->compare('about_me',$this->about_me,true);

		$criteria->compare('register_time',$this->register_time,true);

		$criteria->compare('last_login_time',$this->last_login_time,true);

		$criteria->compare('last_login_ip',$this->last_login_ip,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	

	
	public function saveProfiles($user_id){
		$Profiles = new Profiles;
		$Profiles->user_id = $user_id;
		$Profiles->register_time = $Profiles->last_login_time = time();
		$Profiles->last_login_ip = Yii::app()->request->userHostAddress;
		$Profiles->save();
	}
}